﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using System.Runtime.InteropServices;
using System.IO;
using PDAMain.Entity;
using PDAMain.Forms;
using PDAMain.Interface;

namespace PDAMain.Forms.RepairWork
{
    public partial class RepairWorkDetail : Form
    {
        public string showFlag = "";
        private bool tpFlag = true; //照相标记，控制是否能够保存
        private string status = "";
        public RepairWorkDetail()
        {
            InitializeComponent();
        }

        private void returnToList_Click(object sender, EventArgs e)
        {
            RepairWorkList list = new RepairWorkList();
            list.Show();
            this.Close();
        }

        private void saveData()
        {
            string strRemark = txtProcess.Text.ToString();
            string strDate = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");

            if (DataHelper.RepairWorkDao.deal(DataHelper.RepairWorkDao.AssignmentId, "9", strDate, strRemark))
            {
                MessageBox.Show("成功同步数据至远程数据库");
            }
            else
            {
                if (DataHelper.RepairWorkDao.insertLacolTable(DataHelper.RepairWorkDao.AssignmentId,"9", strDate, strRemark))
                {
                    MessageBox.Show("同步数据失败，保存至本地数据库");
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            backToList();
        }

        private void RepairWorkDetail_Load(object sender, EventArgs e)
        {
            bindData();
        }

        private void bindData()
        {
            DataTable dt = new DataTable();
            dt = DataHelper.RepairWorkDao.getDetail(DataHelper.RepairWorkDao.AssignmentId).Tables[0];
            if (dt.Rows.Count > 0)
            {
                lblWorkNo.Text = dt.Rows[0]["wonum"].ToString();
                lblLocationNo.Text = dt.Rows[0]["location"].ToString();
                lblLocationDescription.Text = dt.Rows[0]["locdescription"].ToString();
                lblAssetNo.Text = DataHelper.RepairWorkDao.AssetNum;
                lblAssetDescription.Text = dt.Rows[0]["assdescription"].ToString();
                lblDescription.Text = dt.Rows[0]["description"].ToString();
                txtProcess.Text = dt.Rows[0]["REMARK"].ToString();
                status = dt.Rows[0]["status"].ToString().Trim();
                Identity.wonum = lblWorkNo.Text.Trim();
            }
        }

        private void toTransferRepair_Click(object sender, EventArgs e)
        {
            if (status == "5")//领料状态不能转单
            {
                MessageBox.Show("已领料状态下不能转单");
            }
            else
            {
                RepairTransfer formShow = new RepairTransfer();
                formShow.Show();
                this.Close();
            }
        }

        private void completed_Click(object sender, EventArgs e)
        {
            //处理完成
           /* if (scanFlag)
            {
                MessageBox.Show("请先完成资产编号扫描再提交");
                return;
            }*/
            if (tpFlag)
            {
                MessageBox.Show("请先完成拍照(三张)再提交");
                return;
            }
            if (txtProcess.Text.Trim() == string.Empty)
            {
                MessageBox.Show("请填写处理意见");
                return;
            }
            if (MessageBox.Show("你确定要执行此操作吗？", "提示框", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            Identity.EndTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            saveData();
        }

        private void RepairWorkDetail_Closed(object sender, EventArgs e)
        {
        }

        private void btnPicture_Click(object sender, EventArgs e)
        {
            string folderName = "Repair";
            string strAssetNo = lblAssetNo.Text.ToString();

            TakePicture tp = new TakePicture();
            string strFileName = "";
            //维修前showFlag为空,维修中为1，维修后为2
            try
            {
                if (showFlag == string.Empty)
                {
                    try
                    {
                        //strFileName = strAssetNo + "_pre";
                        strFileName = DataHelper.RepairWorkDao.AssignmentId + "_pre_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        //tp.Save(strFileName);
                        Bitmap bp = tp.Show(folderName, strFileName);
                        if (bp != null)
                        {
                            pb1.Image = bp;
                            pb1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                            showFlag = "1";
                        }
                    }
                    catch
                    {
                        MessageBox.Show("点击拍照失败，请重新操作");
                    }
                }
                else if (showFlag == "1")
                {
                    try
                    {
                        //strFileName = strAssetNo;
                        strFileName = DataHelper.RepairWorkDao.AssignmentId + DateTime.Now.ToString("yyyyMMddHHmmss");
                        //tp.Save(strFileName);
                        Bitmap bp=tp.Show(folderName, strFileName);
                        if (bp != null)
                        {
                            pb2.Image = bp;
                            pb2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                            showFlag = "2";
                        }
                    }
                    catch
                    {
                        MessageBox.Show("点击拍照失败，请重新操作");
                    }
                }
                else if (showFlag == "2")
                {
                    try
                    {
                        //strFileName = strAssetNo + "_suf";
                        strFileName = DataHelper.RepairWorkDao.AssignmentId + "_suf_" + DateTime.Now.ToString("yyyyMMddHHmmss");
                        //tp.Save(strFileName);
                        Bitmap bp = tp.Show(folderName, strFileName);
                        if (bp != null)
                        {
                            pb3.Image = bp;
                            pb3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                            showFlag = "";
                            tpFlag = false;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("点击拍照失败，请重新操作");
                    }
                }
            }
            catch (Exception ex)
            {
                tp.dispose();
            }
            finally
            {
                tp.dispose();
            }
        }

        private void backToList()
        {
            RepairWorkList list = new RepairWorkList();
            list.Show();
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (NetWorkState.checkWifiState() || NetWorkState.checkGprsState())
            {
                string result = DataHelper.RepairWorkDao.CheckAssetInfo(lblAssetNo.Text.Trim());
                if (result == "")
                {
                    RepairAsset asset = new RepairAsset();
                    asset.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show(result);
                }
            }
            else
            {
                MessageBox.Show("无法连接网络");
            }
        }
    }
}